package com.leetcode;

/**
 * 14. 最长公共前缀
 * 纵向扫描, 不一样的写法
 */
public class Solution14_1 {

    public String longestCommonPrefix(String[] strs) {
        if (strs == null || strs.length == 0) {
            return "";
        }
        int length = strs[0].length();
        int count = strs.length;
        for (int i = 0; i < length; i++) {
            char ch = strs[0].charAt(i);
            for (int j = 1; j < count; j++) {
                // 当前字符串遍历长度到最后一位, 或者当前比较到该位字符不一样
                if (i == strs[j].length() || strs[j].charAt(i) != ch) {
                    return strs[0].substring(0, i);
                }
            }
        }
        return strs[0];
    }

}
